package com.lebang.service;

import com.alibaba.fastjson.JSON;
import com.lebang.domain.ClickStrategyDetail;
import com.lebang.domain.MemberClickRouse;
import com.lebang.domain.TaskRouseClick;
import com.lebang.example.MemberClidkRouseRecordExample;
import com.lebang.example.TaskRouseClickExample;
import com.lebang.mapper.MemberClickRouseMapper;
import com.lebang.mapper.MemberClidkRouseRecordMapper;
import com.lebang.model.Whether;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.List;

/**
 * 签到数据源全局配置
 *
 * @Author hwq
 * @Date 2020-02-17
 */
@Service
public class TaskRouseClickService extends BaseService<TaskRouseClick, Long, TaskRouseClickExample> {
    @Autowired
    private RedisCacheService redisCacheService;
    private static final String KEY = "tb_task_rouse_click";
    @Autowired
    private MemberClidkRouseRecordMapper clidkRouseRecordMapper;
    @Autowired
    private MemberClickRouseMapper clickRouseMapper;
    /**
     * 获取全部数据
     *
     * @return
     */
    public List<TaskRouseClick> findAllList() {
        List<TaskRouseClick> appEatConfigs = redisCacheService.selectListByKey(KEY, TaskRouseClick.class);
        if (appEatConfigs == null) {
            TaskRouseClickExample example = new TaskRouseClickExample();
            example.setOrderByClause("day_count asc");
            example.createCriteria().andHasDeletedEqualTo(Whether.NO.getDbIndex()).andEnableEqualTo(Whether.YES.getDbIndex());
            appEatConfigs = this.selectByExample(example);
            if (appEatConfigs != null) {
                redisCacheService.addCacheListByKey(KEY, JSON.toJSONString(appEatConfigs));
            }
        }
        return appEatConfigs;
    }

    public void delCacheListByKey(Long rouseId) {
        redisCacheService.delCache(KEY);
        redisCacheService.delCache(KEY + ":" + rouseId);
    }

    public TaskRouseClick selectByDays(int days, MemberClickRouse rouses) {
        List<TaskRouseClick> appEatConfigs = this.findAllList();
        for (TaskRouseClick next : appEatConfigs) {
            if (days == next.getDayCount() && rouses.getRouseId().equals(next.getRouseId())) {
                return next;
            }
        }
        return null;
    }

    public TaskRouseClick selectByDaysLess(int days, MemberClickRouse rouses) {
        List<TaskRouseClick> appEatConfigs = this.findAllList();
        for (TaskRouseClick next : appEatConfigs) {
            if (next.getDayCount() >= days && rouses.getRouseId().equals(next.getRouseId())) {
                return next;
            }
        }
        return null;
    }

    public List<TaskRouseClick> findListByRouseId(Long rouseId) {
        List<TaskRouseClick> appEatConfigs = redisCacheService.selectListByKey(KEY + ":" + rouseId, TaskRouseClick.class);
        if (appEatConfigs == null) {
            TaskRouseClickExample example = new TaskRouseClickExample();
            example.setOrderByClause("day_count asc");
            example.createCriteria()
                    .andHasDeletedEqualTo(Whether.NO.getDbIndex())
                    .andEnableEqualTo(Whether.YES.getDbIndex())
                    .andRouseIdEqualTo(rouseId);
            appEatConfigs = this.selectByExample(example);
            if (appEatConfigs != null) {
                redisCacheService.addCacheListByKey(KEY + ":" + rouseId, JSON.toJSONString(appEatConfigs));
            }
        }
        return appEatConfigs;
    }

    /**
     * 签到详情
     * @param rouseClicks
     * @param rouses
     * @param now
     * @return
     */
    public List<TaskRouseClick> filterList(List<TaskRouseClick> rouseClicks, MemberClickRouse rouses, LocalDate now) {
        int k = 0;
        int days =0;
        int t=0;
        for (TaskRouseClick s : rouseClicks) {
            days= (int) now.until(rouses.getCreateTime().plusDays(s.getDayCount()).toLocalDate(), ChronoUnit.DAYS);
            if (rouses.getCreateTime().toLocalDate().plusDays(s.getDayCount()).equals(now)) {
                if (clidkRouseRecordMapper.checkClick(rouses.getMemberId(), rouses.getId(), now)>0) {
                    //签过
                    s.setEnable(0);
                    t=2;
                } else {
                    //可签
                    s.setEnable(1);
                    t=1;
                }
            } else {
                if (rouses.getCreateTime().toLocalDate().plusDays(s.getDayCount()).isAfter(now)) {
                    //还未到
                    if (k == 0) {
                        //下一次签到
                        if(t==1){
                            s.setEnable(3);
                        }else if(t==2){
                            s.setEnable(2);
                        }else {
                            s.setEnable(2);
                        }
                        k = 1;
                    } else {
                        //下一次以后的全部签到
                        s.setEnable(3);
                    }
                    s.setClickDay(days);
                } else {
                    //已过
                    if (clidkRouseRecordMapper.checkClick(rouses.getMemberId(), rouses.getId(), rouses.getCreateTime().toLocalDate().plusDays(s.getDayCount()))>0) {
                        //签过
                        s.setEnable(0);
                    } else {
                        //漏签
                        s.setEnable(-1);
                    }
                }
            }
        }
        return rouseClicks;
    }

    /**
     * 设置为签过 不在体验
     * @param s
     */
    public void cleanClick(MemberClickRouse s) {
        s.setStatus(-1);
        clickRouseMapper.updateByPrimaryKeySelective(s);
    }
    /**
     * 验证最后一天签到是否过了今天
     * @param rouses
     * @return
     */
    public boolean checkLast(MemberClickRouse rouses, long day) {
        LocalDate now =LocalDate.now();
        LocalDate click=rouses.getCreateTime().toLocalDate().plusDays(day);
        if(now.equals(click)){
            return false;
        }else {
            if(now.isAfter(click)){
                cleanClick(rouses);
            }
            return now.isAfter(click);
        }
    }

    /**
     * 获取签到的最后一天是第几天
     * @param clickStrategyDetails
     * @return
     */
    public long getMaxDay(List<ClickStrategyDetail> clickStrategyDetails) {
        long i=0;
        for (ClickStrategyDetail next : clickStrategyDetails) {
            if(next.getDayCount()>=i){
                i=next.getDayCount();
            }
        }
        return i;
    }
    /**
     * 获取签到的最后一天是第几天
     * @param list
     * @return
     */
    public long getMaxDay(List<TaskRouseClick> list,Long rouseId) {
        long i=0;
        for (TaskRouseClick next : list) {
            if(next.getRouseId().equals(rouseId)&&next.getDayCount()>=i){
                i=next.getDayCount();
            }
        }
        return i;
    }

    public int selectCountByClickId(Long id) {
        MemberClidkRouseRecordExample example=new MemberClidkRouseRecordExample();
        example.createCriteria().andClcikRouseIdEqualTo(id);
        return (int) clidkRouseRecordMapper.countByExample(example);
    }
}
